package com.hmall.search.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.common.domain.PageDTO;
import com.hmall.common.utils.BeanUtils;
import com.hmall.search.domain.po.ItemDoc;
import com.hmall.search.domain.query.ItemPageQuery;
import com.hmall.search.service.ISearchService;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SearchServiceImpl implements ISearchService {

    private final RestHighLevelClient restHighLevelClient;

    private final ItemClient itemClient;
    @Override
    public PageDTO<ItemDTO> search(ItemPageQuery query) {
        return null;
    }

    @Override
    public void initEs() throws IOException {

            Integer page = 1;
            final int BATCH_SIZE = 1000;
            final int BULK_SIZE = 5000;  // 每5000条执行一次bulk
            BulkRequest bulkRequest = new BulkRequest();

            while (true) {
                List<ItemDTO> esList = itemClient.getEsList(page, BATCH_SIZE);
                if(CollUtil.isEmpty(esList)) {
                    // 处理最后剩余的数据
                    if (bulkRequest.numberOfActions() > 0) {
                        restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                    }
                    break;
                }

                for (ItemDTO itemDTO : esList) {
                    ItemDoc itemDoc = BeanUtils.toBean(itemDTO, ItemDoc.class);
                    IndexRequest indexRequest = new IndexRequest("items")
                            .id(itemDoc.getId().toString())
                            .source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
                    bulkRequest.add(indexRequest);
                }

                // 当累积到一定数量时才执行bulk操作
                if (bulkRequest.numberOfActions() >= BULK_SIZE) {
                    restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                    bulkRequest = new BulkRequest();
                }
                page++;

        }

    }

    @Override
    public void addORupdate(ItemDoc itemDoc ) throws IOException {
        IndexRequest indexRequest = new IndexRequest();
        if(itemDoc==null){
            return;
        }
        indexRequest.index("items");
        String id = String.valueOf(itemDoc.getId());
        indexRequest.id(id);
        indexRequest.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    }

    @Override
    public void delete(Long id) throws IOException {
        restHighLevelClient.delete(new DeleteRequest("items").id(id.toString()), RequestOptions.DEFAULT);
    }


}
